﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AppMain
{
    public class BoxRole : BaseRole
    {
        public BoxRole(IAttrMap attrMap) : base(attrMap)
        {
        }


        private int lockStatus = 0b00000000;
        public bool IsFree
        {
            get
            {
                return lockStatus == 0;
            }
        }
        public void UnLock(int value)
        {
            lockStatus &= ~value;
        }
        public void Lock(int value)
        {
            lockStatus = lockStatus | value;
        }

        public DynamicBox3D box3D = new DynamicBox3D(0, 0, 0, 0, 0, 0, 0, 0, 0);
        public IMoveWidget moveWidget;
        public float RoleHeight
        {
            get
            {
                return this.box3D.posY;
            }
            set
            {
                this.box3D.updatePos(this.box3D.posX, value, this.box3D.posZ);
            }
        }

        protected override void initialize()
        {
            base.initialize();
            int roleTempId = this.AttrMap.getAttr<int>(RoleAttr.RoleTempId).Value;
            if (roleTempId > 0)
            {
                initBox(roleTempId);
            }
            initMoveWidget();
        }

        protected virtual void initMoveWidget()
        {
            this.moveWidget = new RoleMoveWidget();
            this.moveWidget.initialize(this);
        }

        protected override void initAttrListener()
        {
            base.initAttrListener();
            this.AttrMap.getAttr<int>(RoleAttr.RoleTempId).onAttrChange += onRoleTempChange;
        }

        protected void onRoleTempChange(int tempId)
        {
            if (tempId > 0)
                this.initBox(tempId);
        }

        public virtual void move(float x, float z)
        {
            this.box3D.updatePos(box3D.posX + x, this.RoleHeight, box3D.posZ + z);
        }
        public void movePos(float x, float z)
        {
            this.box3D.updatePos(x, this.RoleHeight, z);
        }

        protected void initBox(int roleType)
        {
            //设置角色碰撞
            string sql = string.Format("select *from tb_role_temp where id='{0}'", roleType);
            var reader = SqlMgr.Instance.select(sql);
            if (reader.Read())
            {
                string boxSizeStr = reader[2].ToString();
                string boxOffsetStr = reader[3].ToString();
                float[] boxSize = Helper.split2Float(boxSizeStr);
                float[] boxOffset = Helper.split2Float(boxOffsetStr);
                box3D.updateSize(boxSize);
                box3D.updateOffset(boxOffset);
            }
            reader.Close();
        }

        public override void tick(float frameTime)
        {
            if (this.moveWidget != null)
                this.moveWidget.tick(frameTime);
        }

    }
}
